package com.geeksx.trainer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.geeksx.trainer.entity.vo.QuestionVO;
import com.geeksx.trainer.mapper.QuestionMapper;
import com.geeksx.trainer.mapper.QuestionRecordMapper;
import com.geeksx.trainer.service.PracticeService;
import com.geeksx.trainer.entity.Question;
import com.geeksx.trainer.entity.QuestionRecord;
import com.geeksx.trainer.entity.request.PracticeStartRequest;
import com.geeksx.trainer.entity.request.PracticeSubmitRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 答题练习服务实现
 */
@Service
@RequiredArgsConstructor
public class PracticeServiceImpl implements PracticeService {

    private final QuestionMapper questionMapper;
    private final QuestionRecordMapper recordMapper;

    @Override
    public List<QuestionVO> startPractice(PracticeStartRequest request) {
        // 根据不同模式获取题目
        if (request.getStudyModeId() == 0){
            return questionMapper.selectAllQuestions(request);
        }

        return questionMapper.selectRandomQuestions(request);
    }

    @Override
    public QuestionVO getRecommendQuestion(PracticeStartRequest request) {
        return questionMapper.getRecommendQuestion(request);
    }

    @Override
    public Map<String, Object> submitAnswer(PracticeSubmitRequest request) {
        Question question = questionMapper.selectById(request.getQuestionId());
        boolean isCorrect = question.getAnswer().equals(request.getUserAnswer());

        // 保存答题记录
        QuestionRecord record = new QuestionRecord();
        record.setUserId(request.getUserId());
        record.setStudyModeId(request.getStudyModeId());
        record.setQuestionTypeId(request.getQuestionTypeId());
        record.setQuestionId(request.getQuestionId());
        record.setUserAnswer(request.getUserAnswer().toString());
        record.setIsCorrect(isCorrect ? 1 : 0);
        record.setTimeSpent(request.getTimeSpent());
        recordMapper.insert(record);

        Map<String, Object> result = new HashMap<>();
        result.put("isCorrect", isCorrect);
        result.put("correctAnswer", question.getAnswer());
        result.put("explanation", question.getExplanation());
        return result;
    }

    @Override
    public Map<String, Object> getHistory(Long userId, Long studyModeId) {
        List<QuestionRecord> records = recordMapper.selectList(
                new LambdaQueryWrapper<QuestionRecord>()
                        .eq(QuestionRecord::getUserId, userId)
                        .eq(QuestionRecord::getStudyModeId, studyModeId)
        );

        long total = records.size();
        long correct = records.stream().filter(r -> r.getIsCorrect() == 1).count();

        Map<String, Object> result = new HashMap<>();
        result.put("total", total);
        result.put("correct", correct);
        result.put("accuracy", total == 0 ? 0 : (double) correct / total);
        result.put("records", records);
        return result;
    }
}
